package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.LikesBehaviorDto;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApTheadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Queue;

/**
 * @author shitou
 * @className ApLikesBehaviorServiceImpl
 * @description: TODO
 */
@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        log.info("点赞功能执行成功");
        // 校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getArticleId() == null || dto.getArticleId() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 用户未登录
        ApUser apUser = ApTheadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 游客需要进行登录，才能进行用户行为
        if (apUser.getId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String userLikesArticleId = "USER_LIKES_" + dto.getArticleId();

        // 查询redis中当前用户是否点过赞
        Boolean likeFlag = stringRedisTemplate.opsForHash().hasKey(userLikesArticleId, apUser.getId() + "");
        //  用户没有点过赞,进行点赞行为    0 点赞 1 取消点赞
        if (!likeFlag && dto.getOperation() == 0) {
            // 点赞成功存入redis
            stringRedisTemplate.opsForHash().put(userLikesArticleId, apUser.getId() + "", "1");
            // 存入mongo
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setUserId(apUser.getId().longValue());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType(dto.getType());
            mongoTemplate.insert(apLikesBehavior);
        }
        // 用户取消点赞，删除redis mongo中数据
        if (likeFlag && dto.getOperation() == 1) {
            stringRedisTemplate.opsForHash().delete(userLikesArticleId, apUser.getId() + "");
            Query query = Query.query(Criteria.where("userId").is(apUser.getId()).and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query, ApLikesBehavior.class);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
